package com.yiwei.jmx.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.yiwei.jmx.R;
import com.yiwei.jmx.api.Api;
import com.yiwei.jmx.api.bean.LoginBean;
import com.yiwei.jmx.base.BaseActivity;
import com.yiwei.jmx.constant.UserInfoGlobal;
import com.yiwei.jmx.util.MD5;
import com.yiwei.jmx.util.Network;
import com.yiwei.jmx.util.RxLifeUtil;
import com.yiwei.jmx.util.ToastUtil;
import com.yiwei.jmx.util.UI;
import com.yiwei.jmx.util.Util;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class LoginByPassActivity extends BaseActivity {


    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.btn_wechat)
    TextView btnWechat;
    @BindView(R.id.edit_user)
    EditText editUser;
    @BindView(R.id.edit_pass)
    EditText editPass;
    @BindView(R.id.btn_find)
    TextView btnFind;
    @BindView(R.id.btn_submit)
    TextView btnSubmit;
    @BindView(R.id.btn_login_by_code)
    TextView btnLoginByCode;
    @BindView(R.id.btn_agree)
    TextView btnAgree;
    @BindView(R.id.btn_account_clear)
    ImageView btnAccountClear;
    @BindView(R.id.btn_pass_clear)
    ImageView btnPassClear;
    @BindView(R.id.btn_pass_open)
    ImageView btnPassOpen;
    private Unbinder unbiner;
    private boolean pass_isChecked = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_by_pass);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();

    }

    private void init() {

    }

    @OnTextChanged({R.id.edit_user, R.id.edit_pass})
    void onTextChange(Editable s) {
        if (!TextUtils.isEmpty(UI.toString(editUser)) && !TextUtils.isEmpty(UI.toString(editUser))) {
            btnSubmit.setSelected(true);
        } else {
            btnSubmit.setSelected(false);
        }
    }

    public void getLogin(String account, String pass) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }
        loadingDialog.show();
        LoginBean bean = new LoginBean();
        bean.account = account;
        bean.password = MD5.getMD5str(pass);
        Api.api_service.getLogin(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    UserInfoGlobal.login();
                    finish();
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        unbiner.unbind();
        unbiner = null;
    }

    @OnClick({R.id.btn_pass_open, R.id.btn_account_clear, R.id.btn_pass_clear, R.id.btn_back, R.id.btn_wechat, R.id.btn_find, R.id.btn_submit, R.id.btn_login_by_code, R.id.btn_agree})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_account_clear:
                editUser.setText("");
                break;
            case R.id.btn_pass_clear:
                editPass.setText("");
                break;
            case R.id.btn_back:
                finish();
                break;

            case R.id.btn_pass_open:
                pass_isChecked = !pass_isChecked;
                if (pass_isChecked) {
                    //如果选中，显示密码
                    editPass.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    btnPassOpen.setImageResource(R.mipmap.img_eye_open);
                } else {
                    //否则隐藏密码
                    editPass.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    btnPassOpen.setImageResource(R.mipmap.img_eye_close);
                }
                break;
            case R.id.btn_wechat:
                Util.startActivity(this, WechatChatActivity.class);
                break;
            case R.id.btn_find:
                Util.startActivity(this, ForgetPassActivity.class);
                break;
            case R.id.btn_submit:
                if (TextUtils.isEmpty(UI.toString(editUser))) {
                    ToastUtil.makeText(this, "账号不能为空");
                    return;
                }
                if (TextUtils.isEmpty(UI.toString(editPass))) {
                    ToastUtil.makeText(this, "密码不能为空");
                    return;
                }
                getLogin(UI.toString(editUser), UI.toString(editPass));
                break;
            case R.id.btn_login_by_code:
                finish();
                break;
            case R.id.btn_agree:
                break;
        }
    }
}
